/*
 *      Author: Guannan Ma
 *      @mythmgn
 *      Whatever license applied
 */

#include <stdio.h>
#include "base.h"

#ifndef BASE_LIB_LIBMISC_H
#define BASE_LIB_LIBMISC_H

#ifdef __cplusplus
extern "C"
{
#endif


    typedef struct lineInfo {
        int lineNum; // This is the next line it will read
        //   So the current_line= lineNum-1;

        long nowPos;
    } lineInfo;


    void
        skipComments(FILE *file);

    /* strdtb(s) - delete trailing blanks in string 's' and return new length
    */
    int
        strdtb(char *s);

    /** 
      @brief        Check whether the pointed line is a line with a # in the head
      @return       If it is commented by # in the head, return 1, else return 0.
      @param [in]   line   The very line you want to check
     *
     */
    BOOL
        isContainSign(char *line);

    /**
     * @brief read a line from the FILEW into outputLine. li will record the next line it will read 
     *        and the position of the file.
     * @param [in] fw   : FILEW*
     * @param [out] outputLine   : char*
     * @param [out] li   : lineInfo*
     * @return  EXBOOL 
     * @retval  If error happens, return ERROR. If the file is ended. return FALSE
     Otherwise, return TRUE;
     **/
    EXBOOL
        readLine(FILEW *fw,char *outputLine,lineInfo *li);

    /*!
     * @fn           getKeyAndValue
     @brief        Get a key and corresponding value of a line of configuration file
     @return
     The line string should be formatted like this "%s = %s #"
     Otherwise, it will return 0. Successfully done will return 1.
     @param line   The very line you want to get key and value from
     @param key    The string on the left of "=" will be stored to where key points.
     @param value  The string on the right side of =
     *
     */
    BOOL
        getKeyAndValue(const char *line,char *key,char *value);
    int 
        strdlb(char *s);

    uint64 
        splitstring(const char *buf, const char c, char *arr[], int n);

    struct flag {
        long long	 f_flag;
        char		*f_str;
    };

    uint64
        str2flags(struct flag *tflags, char *sflags);
    char *
        flags2str(struct flag *tflags, long long flags);


#ifdef __cplusplus
}
#endif

#endif
